package com.qhx.rtkcloud.tcp;

//import javafx.util.Pair;
import org.springframework.util.Base64Utils;
import org.springframework.util.StringUtils;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.LinkedBlockingQueue;

public class NtripTransferPair {
//    public static void main(String[] args) throws IOException {
//        new NtripTransferPair();
//    }
//
//    private Map<String, String> portPair = new HashMap<>();
//
//    private Map<String, NtripExchangeUnit> ntripExchangeUnitMap = new ConcurrentHashMap<>();
//
//
//    {
//        portPair.put("mount1", "RTCM23");
//        portPair.put("mount2", "mount1");
//    }
//
//
//
//    private String getMountFromServer(String info) {
//        int slash = info.indexOf("/");
//        int lineBreak = info.indexOf("\n");
//        if (slash <= 0 || lineBreak <= 0 || slash >= lineBreak) {
//            return null;
//        }
//        return info.substring(slash + 1, lineBreak);
//    }
//
//    private String getMountFromClient(String info) {
//        int slash = info.indexOf("/");
//        int lineBreak = info.indexOf("\n");
//        int http = info.indexOf("HTTP");
//        if (slash <= 0 || lineBreak <= 0 || http <= 0 || slash >= lineBreak || slash >= http) {
//            return null;
//        }
//        return info.substring(slash + 1, http).trim();
//    }
//
//    private String decodeBase64(String base64) {
//        if (StringUtils.isEmpty(base64)) {
//            return null;
//        }
//        byte[] bytes = Base64Utils.decode(base64.getBytes());
//        return new String(bytes);
//    }
//
//    private String getUsername(String usernameAndPwd) {
//        if (usernameAndPwd == null || usernameAndPwd.length() == 0) {
//            return null;
//        }
//
//        int colon = usernameAndPwd.indexOf(":");
//
//        if (colon <= 0) {
//            return null;
//        }
//
//        return usernameAndPwd.substring(0, colon);
//
//    }
//
//    private String getUsernameFromClient(String info) {
//
//        String startStr = "Anthorization: Base";
//        int indexStart = info.indexOf(startStr);
//        if (indexStart <= 0) {
//            return null;
//        }
//        int lineBreak = info.indexOf("\n", indexStart);
//
//        return getUsername(decodeBase64(info.substring(indexStart + startStr.length(), lineBreak).trim()));
//
//
//    }
//
//
//    private Pair<String, Boolean> getMounterAndUsername(String registerInfo) {
//        String mount = null;
//        String username = null;
//
//        if (registerInfo.startsWith("SOURCE")) {// 基准站过来 Server,需要直接返回成功
//            mount = getMountFromServer(registerInfo);
//            return mount == null ? null : new Pair<>(mount, true);
//        } else if (registerInfo.startsWith("GET")) {   // 移动站client
//            mount = getMountFromClient(registerInfo);
//            username = getUsernameFromClient(registerInfo);
//            return mount == null ? null : new Pair<>(username == null ? mount : mount + "&" + username, false);
//        }
//        return null;
//    }
//
//    public NtripTransferPair() throws IOException {
//        ServerSocket serverSocket = new ServerSocket(1011);
//        for (Map.Entry<String, String> entry : portPair.entrySet()) {
//            NtripExchangeUnit ntripExchangeUnit1 = new NtripExchangeUnit(new LinkedBlockingQueue<>());
//            ntripExchangeUnit1.setMount(entry.getKey());
//            NtripExchangeUnit ntripExchangeUnit2 = new NtripExchangeUnit(new LinkedBlockingQueue<>());
//            ntripExchangeUnit2.setMount(entry.getValue());
//            ntripExchangeUnit1.setReadQueue(ntripExchangeUnit2.getWriteQueue());
//            ntripExchangeUnit2.setReadQueue(ntripExchangeUnit1.getWriteQueue());
//            ntripExchangeUnit1.setPartner(ntripExchangeUnit2);
//            ntripExchangeUnit2.setPartner(ntripExchangeUnit1);
//            ntripExchangeUnit1.setWriteRestrained(true);
//            ntripExchangeUnit1.setWriteRestrained(false);
//            ntripExchangeUnitMap.put(entry.getKey(), ntripExchangeUnit1);
//            ntripExchangeUnitMap.put(entry.getValue(), ntripExchangeUnit2);
//        }
//
//        while (true) {
//            Socket client = serverSocket.accept();
//            OutputStream outputStream = client.getOutputStream();
//            InputStream inputStream = client.getInputStream();
//            PrintWriter out = new PrintWriter(outputStream, true);
//
//            byte[] bytes = new byte[10240];
//            int length = 0;
//            if ((length = inputStream.read(bytes)) > 0) {
//                String registerInfo = new String(Arrays.copyOfRange(bytes, 0, length));
//                Pair<String, Boolean> pair = getMounterAndUsername(registerInfo);
//                // 将数据更新到NtripExchangeUnit;
//                NtripExchangeUnit ntripExchangeUnit = ntripExchangeUnitMap.get(pair.getKey());
//                if (ntripExchangeUnit == null) {
//                    break;
//                }
//                ntripExchangeUnit.setClient(client);
//                ntripExchangeUnit.setInputStream(inputStream);
//                ntripExchangeUnit.setOutputStream(outputStream);
//                if (pair.getValue()) {
//                    outputStream.write("ICY 200 OK".getBytes());
//                    outputStream.flush();
//                }
//                ntripExchangeUnit.start();
//
//            }
//        }
//
//
//    }
}
